//
//  WmcUploadStatusView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/28.
//

import UIKit

class WmcUploadStatusView: WmcBaseView {

    enum Status: Int {
    case uploading
    case failure
    }
    
    var status: Status = .failure {
        didSet {
            WmcUploadQueueV2DBManager.query { [weak self] list in
                guard let weakSelf = self else { return }
                let count = list.count

                if weakSelf.status == .uploading {
                    weakSelf.iconImageView.isHidden = true
                    weakSelf.detailBtn.isHidden = true
                    weakSelf.retryBtn.isHidden = true

                    weakSelf.textLabel.text = "正在同步（\(count)）…"

                    weakSelf.textLabel.snp.updateConstraints { make in
                        make.left.equalTo(16)
                    }
                } else if weakSelf.status == .failure {
                    weakSelf.iconImageView.isHidden = false
                    weakSelf.detailBtn.isHidden = false
                    weakSelf.retryBtn.isHidden = false

                    weakSelf.textLabel.text = "同步失败（\(count)）"

                    weakSelf.textLabel.snp.updateConstraints { make in
                        make.left.equalTo(43)
                    }
                }
            }
        }
    }

    override func makeUI() {
        backgroundColor = .wmc_blue_DFE9FA
        
        addSubview(iconImageView)
        addSubview(textLabel)
        addSubview(detailBtn)
        addSubview(retryBtn)
        
        iconImageView.snp.makeConstraints { make in
            make.left.equalTo(13)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        textLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(43)
            make.right.equalTo(-146)
        }
        
        detailBtn.snp.makeConstraints { make in
            make.width.equalTo(56)
            make.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.right.equalTo(retryBtn.snp.left).offset(-8)
        }
        
        retryBtn.snp.makeConstraints { make in
            make.width.equalTo(56)
            make.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
        }
    }
    
    override func bindViewModel() {
        WmcUploadQueueManager.shared.taskUploadSuccessSubject.subscribe(onNext: { [weak self] model in
            guard let weakSelf = self else { return }
            
            WmcUploadQueueV2DBManager.query { list in
                let count = list.count
                weakSelf.textLabel.text = "正在同步（\(count)）…"
            }
        }).disposed(by: rx.disposeBag)
    }
    
    override func bindViewAction() {
        detailBtn.yp.addEvent { _ in
            WmcNavigatorManager.push(.uploadFailure)?.hidesBottomBarWhenPushed = true
        }
        
        retryBtn.yp.addEvent { _ in
            WmcUploadQueueManager.startAutoUploadTask()
        }
    }
    
    private lazy var iconImageView: UIImageView = {
        let view = UIImageView()
        view.image = "upload_failure_notice".wmc_image
        return view
    }()
    
    private lazy var textLabel: UILabel = {
        let label = UILabel()
        label.font = 14.wmc_font
        label.textColor = .wmc_mainTheme
        return label
    }()
    
    private lazy var detailBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("查看", for: .normal)
        btn.setTitleColor(.wmc_mainTheme, for: .normal)
        btn.titleLabel?.font = 12.wmc_font
        btn.layer.borderColor = UIColor.wmc_mainTheme.cgColor
        btn.layer.borderWidth = 1
        btn.layer.cornerRadius = 4
        return btn
    }()
    
    private lazy var retryBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("重试", for: .normal)
        btn.setTitleColor(.wmc_white_FFFFFF, for: .normal)
        btn.titleLabel?.font = 12.wmc_font
        btn.setBackgroundImage(UIColor.wmc_mainTheme.yp.uiImage, for: .normal)
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        return btn
    }()

}
